package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {

    /*配置Redis模板*/
    @Bean
    @ConditionalOnMissingBean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        /*日志记录开始创建RedisTemplate对象*/
        log.info("开始创建RedisTemplate对象...");
        /*实例化RedisTemplate对象 用于执行redis中的各种操作*/
        RedisTemplate redisTemplate = new RedisTemplate();
        /*为RedisTemplate设置ConnectionFactory，用于建立与Redis服务器的连接*/
        /*设置redis的链接工厂对象*/
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        /*设置Redis键的序列化方式为StringRedisSerializer，确保键的序列化一致性*/
        /*设置redis key的序列化器*/
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        /*返回配置好的RedisTemplate对象，供其他组件使用*/
        return redisTemplate;
    }

}
